package com.example.lwj.utils;

import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.view.View;

import com.example.lwj.application.MyAplication;

/**
 * Created by lwj on 2016/10/31.
 */
public class CommonUtls {

    private static View view;
    private static String textId;
    private static final String Tag = "TopNews";
    private static SharedPreferences sharedPreferences;

    //inflate 打气  也就是创建个布局
    public static View inflate(int layoutId) {
        //加载资源文件
        view = View.inflate(MyAplication.getContext(), layoutId, null);
        return view;
    }

    //图片的准换dip--px
    //px是绝对像素 dip是相对像素
    //像素密度的算法：
    public static int dip_px(int dip) {
        //获取像素的密度
        float density = MyAplication.getContext().getResources().getDisplayMetrics().density;
        int px = (int) (dip * density + 0.5f);
        return px;
    }

    //图片的准换px_dip
    //像素密度的算法：
    public static int px_dis(int px) {
        //获取像素的密度
        float density = MyAplication.getContext().getResources().getDisplayMetrics().density;
        int dip = (int) (px * density + 0.5f);
        return dip;
    }

    //textId
    public static String getString(int stringId) {
        return MyAplication.getContext().getResources().getString(stringId);
    }

    //图片
    public static Drawable getDrawable(int did) {
        return MyAplication.getContext().getResources().getDrawable(did);
    }

    //shardpreferces公用
    public static void getsp(String flag, String string) {
        if (sharedPreferences == null) {
            sharedPreferences = MyAplication.getContext().getSharedPreferences(Tag, MyAplication.getContext().MODE_PRIVATE);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(flag, string);
        edit.commit();
    }

    public static String getSp(String flag) {
        if (sharedPreferences == null) {
            sharedPreferences = MyAplication.getContext().getSharedPreferences(Tag, MyAplication.getContext().MODE_PRIVATE);
        }
        return sharedPreferences.getString(flag, "");

    }

    //判断当前是主线程还是子线程
    public static void runOnUiThread(Runnable runOnUiThread) {
        if (android.os.Process.myTid() == MyAplication.getMainThreadId()) {
            runOnUiThread.run();
        } else {
            MyAplication.getHandler().post(runOnUiThread);
        }
    }

    //执行
    public static void exrun(Runnable runnable) {
        //使用线程池
        MyAplication.getPool().execute(runnable);
    }
}
